///<reference types="@i3yun/viewer" />
import { Container } from '../Container';
import { BaseGui } from "../Services/BaseGui";
import { NodeOffMan } from '../Services/GroupManService';
import { ViewerService } from "../Services/ViewerService";
export class ModelToolGui extends BaseGui {
    title = `模型工具[${ModelToolGui.name}]`;
    viewerService = Container.defineProperty(ViewerService);

    protected UpdateGui(): void {
        let fun = {
            focus: () => {
                const array = new Float32Array(6);
                const point = new THREE.Vector3();
                const box = new THREE.Box3();
                this.viewerService.viewer.getAllModels().forEach(m => {
                    let t = m.getInstanceTree();
                    m.selector.getSelection().forEach(_ => {
                        t.getNodeBox(_, array);
                        point.fromArray(array as any);
                        box.expandByPoint(point);
                        point.fromArray(array as any, 3);
                        box.expandByPoint(point);
                    });
                });
                this.viewerService.viewer.navigation.fitBounds(false, box);
            },
            isolate: () => {
                // var selection = viewer.getAggregateSelection();
                // viewer.clearSelection();
                // viewer.impl.visibilityManager.aggregateIsolate(selection,{});

                this.viewerService.viewer.getAllModels().forEach((model) => {
                    let select = model.selector.getSelection();
                    // model.visibilityManager.setAllVisibility(true);
                    // model.visibilityManager.isolateNone();
                    if (select.length > 0)
                        model.visibilityManager.isolate(select);
                    else
                        model.visibilityManager.setAllVisibility(false);
                    model.selector.clearSelection();
                });
            },
            transparent: () => {
                this.viewerService.viewer.getAllModels().forEach(model => {
                    model.visibilityManager.hide(model.selector.getSelection());
                    model.selector.clearSelection();
                });
            },
            unTransparent: () => {
                let selections = this.viewerService.viewer.getAllModels().map(model => {
                    return { model: model, selection: model.selector.getSelection() };
                }).filter(_ => _.selection.length > 0);
                if (selections.length > 0) {
                    selections.forEach(({ model, selection }) => {
                        model.visibilityManager.show(selection);
                        model.selector.clearSelection();
                    });
                }
                else {
                    this.viewerService.viewer.getAllModels().forEach(model => {
                        model.visibilityManager.show(model.visibilityManager.getHiddenNodes());
                        model.visibilityManager.isolateNone();
                    });
                }
            },
            hide: () => {
                this.viewerService.viewer.getAllModels().forEach(model => {
                    this.getMan(NodeOffMan, model).off(model.selector.getSelection());
                    model.selector.clearSelection();
                });
            },
            unHide: () => {
                let selections = this.viewerService.viewer.getAllModels().map(model => {
                    return { model: model, selection: model.selector.getSelection() };
                }).filter(_ => _.selection.length > 0);

                if (selections.length > 0) {
                    selections.forEach(({ model, selection }) => {
                        this.getMan(NodeOffMan, model).unOff(selection);
                        model.selector.clearSelection();
                    });
                }
                else {
                    this.viewerService.viewer.getAllModels().forEach(model => {
                        this.getMan(NodeOffMan, model).clear();
                    });
                }
            },
            showAll: () => {
                this.viewerService.viewer.getAllModels().forEach(model => {
                    model.visibilityManager.show(model.visibilityManager.getHiddenNodes());
                    model.visibilityManager.isolateNone();
                    this.getMan(NodeOffMan, model).clear();
                    model.selector.clearSelection();
                });
            }
        }
        let updateGUI = () => {
            Array.from(this.gui.__controllers).forEach(_ => {
                this.gui.remove(_);
            });
            this.guiService.buildGUIByObject(this.gui, fun);
        }
        updateGUI();
    }
    private getMan<T>(value: { new(model: Sippreep.Viewing.Model, viewer: Sippreep.Viewing.Viewer3D): T }, model: Sippreep.Viewing.Model | any) {
        if (!model[value.name]) {
            model[value.name] = new value(model, this.viewerService.viewer);
        }
        return model[value.name] as T;
    }
}
